﻿using System;
using System.Collections.Generic;
using System.Text;

namespace net.veekee.common
{
    public class FS
    {
        public static void CheckOptionalizedPath(String id)
        {
            String option = Options.GetInstance().Get(id);
            if (option.Length == 0)
                return;
            if (option.EndsWith("\""))
            {
                option = option.Remove(option.Length - 1);
                Options.GetInstance().Set(id, option);
            }
            if (option.EndsWith("" + System.IO.Path.DirectorySeparatorChar))
            {
                option = option.Remove(option.Length - 1);
                Options.GetInstance().Set(id, option);
            }
            if (!System.IO.Directory.Exists(option))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(option);
                }
                catch (Exception e)
                {
                    Log.GetInstance().Error("Couldn't create '{0}' directory: {1}", option, e.ToString());
                }
            }
        }

        public static String ManageWorkingDir(String path)
        {
            if (path == null)
                return null;
            if (path.Length > 3 && path[1] == ':')
                return path;
            return Options.GetInstance().Get("pwd", "") + System.IO.Path.DirectorySeparatorChar + path;
        }
    }
}
